package com.bootdo.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.system.domain.DlNewsCategoryDO;
import com.bootdo.system.service.DlNewsCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.DlNewsDO;
import com.bootdo.system.service.DlNewsService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-06-04 15:08:59
 */
 
@Controller
@RequestMapping("/system/dlNews")
public class DlNewsController {
	@Value("${upload.IP}")
	private String IP;
	@Autowired
	private DlNewsService dlNewsService;
	@Autowired
	private DlNewsCategoryService dlNewsCategoryService;

	@GetMapping()
	@RequiresPermissions("system:dlNews:dlNews")
	String DlNews(){
	    return "system/dlNews/dlNews";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:dlNews:dlNews")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DlNewsDO> dlNewsList = dlNewsService.list(query);
		int count = dlNewsService.count(query);
		PageUtils pageUtils = new PageUtils(dlNewsList, count);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:dlNews:add")
	String add(Model model){
		List<DlNewsCategoryDO> list = dlNewsCategoryService.list(new HashMap<>());
		model.addAttribute("list", list);
		return "system/dlNews/add";
	}

	@GetMapping("/edit/{newsId}")
	@RequiresPermissions("system:dlNews:edit")
	String edit(@PathVariable("newsId") Integer newsId,Model model){
		DlNewsDO dlNews = dlNewsService.get(newsId);
		List<DlNewsCategoryDO> list = dlNewsCategoryService.list(new HashMap<>());
		if(dlNews.getTitlePhoto() != null && !dlNews.getTitlePhoto().equals("")){
			dlNews.setTitlePhoto(IP + "/upload/" + dlNews.getTitlePhoto());
		}
		if(dlNews.getBannerPhoto() != null && !dlNews.getBannerPhoto().equals("")){
			dlNews.setBannerPhoto(IP + "/upload/" + dlNews.getBannerPhoto());
		}
		if(dlNews.getCarouselPhoto() != null && !dlNews.getCarouselPhoto().equals("")){
			dlNews.setCarouselPhoto(IP + "/upload/" + dlNews.getCarouselPhoto());
		}
		model.addAttribute("dlNews", dlNews);
		model.addAttribute("list", list);
	    return "system/dlNews/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:dlNews:add")
	public R save( DlNewsDO dlNews){
		if("".equals(dlNews.getTitlePhoto())){
			return R.error("请上传标题图");
		}
		if("1".equals(dlNews.getBannerFlag()) && (dlNews.getBannerPhoto() == null || dlNews.getBannerPhoto().equals(""))){
			return R.error("请上传banner图");
		}
		if("1".equals(dlNews.getCarouselFlag()) && (dlNews.getCarouselPhoto() == null || dlNews.getCarouselPhoto().equals(""))){
			return R.error("请上传轮播图");
		}
		if(dlNewsService.save(dlNews)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:dlNews:edit")
	public R update( DlNewsDO dlNews){
		if("".equals(dlNews.getTitlePhoto())){
			return R.error("请上传标题图");
		}
		if("1".equals(dlNews.getBannerFlag()) && (dlNews.getBannerPhoto() == null || dlNews.getBannerPhoto().equals(""))){
			return R.error("请上传banner图");
		}
		if("1".equals(dlNews.getCarouselFlag()) && (dlNews.getCarouselPhoto() == null || dlNews.getCarouselPhoto().equals(""))){
			return R.error("请上传轮播图");
		}
		dlNewsService.update(dlNews);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:dlNews:remove")
	public R remove( Integer newsId){
		if(dlNewsService.remove(newsId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:dlNews:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] newsIds){
		dlNewsService.batchRemove(newsIds);
		return R.ok();
	}
	
}
